﻿using Hangfire;
//using Hangfire.Console;
using Hangfire.Dashboard;
using Hangfire.Dashboard.BasicAuthorization;
using Hangfire.HttpJob;
using Hangfire.SqlServer;
//using Hangfire.Tags.SqlServer;
using Microsoft.Extensions.Options;
using Hangfire.Storage.SQLite;
using System.Linq;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Hangfire_Demo.HFConfig;
using Hangfire.Common;
using Hangfire_Demo.HFCustom;

namespace HFConfig
{
    public static class HangfireConfigExt
    {

        public static void CustomAddHangfire(this WebApplicationBuilder builder)
        {
            #region 配置Hangfire
          //添加 Hangfire配置
          builder.Services.AddHangfire(configuration => configuration
               .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
               .UseSimpleAssemblyNameTypeSerializer()
               .UseRecommendedSerializerSettings()
               //.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions() //Nuget引入：
               //{
               //    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
               //    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
               //    QueuePollInterval = TimeSpan.Zero,
               //    UseRecommendedIsolationLevel = true,
               //    UsePageLocksOnDequeue = true,
               //    DisableGlobalLocks = true

               //}
               .UseSQLiteStorage("Hangfire.db", new SQLiteStorageOptions()
               {
                   AutoVacuumSelected = SQLiteStorageOptions.AutoVacuum.FULL,
                   JobExpirationCheckInterval = TimeSpan.FromSeconds(30)
               })
               

                 //.UseTagsWithSql()//nuget引入Hangfire.Tags.SqlServer
                 //.UseConsole(new ConsoleOptions()
                 //{
                 //    BackgroundColor = "#000079"

                 //})
                 ///////这里修改未生效
                 .UseDefaultCulture(new System.Globalization.CultureInfo("en-US", false),
                 new System.Globalization.CultureInfo("en-US", false), false) ////"en-US", "zh-CN",
                 );

            //全局过滤器 注册 特性 
            //GlobalJobFilters.Filters.Add(new LogEverythingAttribute());
            //GlobalJobFilters.Filters.Add(new CustomApplyStateFilter());

            builder.Services.AddHangfireServer(); //Hangfire的服务也添加到容器中去

            #endregion
            #region HttpJob
/*            // 支持HttpJob:
            //1.nuget引入:Hangfire.HttpJob
            //2. .UseHangfireHttpJob(
            builder.Services.AddHangfire(configuration => configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions()
                {
                    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    QueuePollInterval = TimeSpan.Zero,
                    UseRecommendedIsolationLevel = true,
                    UsePageLocksOnDequeue = true,
                    DisableGlobalLocks = true
                })
                .UseTagsWithSql()
                .UseConsole(new ConsoleOptions { BackgroundColor = "#000079" })
                //支持httpJob
                .UseHangfireHttpJob()
                );        
            builder.Services.AddHangfireServer(); //Hangfire的服务也添加到容器中去*/
            #endregion
        }


        public static void UseCustomHangfire(this WebApplication app)
        {
            #region 默认的申请认证 配置使用面板
            //可视化面板的授权--集成ASP.NET Core授权
/*            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                //启用所有的权限
                IsReadOnlyFunc = (DashboardContext context) => true,
                //配置权限
                Authorization = new CustomDashboardAuthorizationFilter[] { new CustomDashboardAuthorizationFilter() },
                DashboardTitle = "Hello-Hangfire"
            });*/
            #endregion


            #region hangfire自定义授权
            //读取授权信息
            User user = app.Services.GetRequiredService<IOptionsMonitor<User>>().CurrentValue;
            BasicAuthAuthorizationUser[] userList = user.BasicAuthAuthorizationUser
                .Select(c => new BasicAuthAuthorizationUser()
                {
                    Login = c.Name,
                    PasswordClear = c.Password
                }).ToArray();
            //支持可视化界面 ---任何一个用户都能够来访问；所以需要加一道屏障
            app.UseHangfireDashboard("/hangfire", new DashboardOptions()
            {
                //这里是默认实现，如果使用这个，小心坑：nuget引入：Hangfire.Dashboard.BasicAuthorization
                Authorization = new BasicAuthAuthorizationFilter[]
                {
                    new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
                    {
                        SslRedirect=false,
                        RequireSsl=false,
                        Users=userList
                    })
                },
                //////自定义 完成的作业 作业 显示名称，同一个方法可以根据调用参数判断
                DisplayNameFunc = new CustomDisplayNameFunc().GenerateDisplayName


            });
            var jobOptions = new BackgroundJobServerOptions
            {
                //Queues = new[] {"default" },          //队列名称，只能为小写
                //ServerName = "HangFire",  //服务器名称

                //检测时间 默认15秒 改为 1秒
                SchedulePollingInterval = TimeSpan.FromMilliseconds(1000),
                //心跳时间
                HeartbeatInterval = TimeSpan.FromMilliseconds(5000),
                //总工作数 并发任务数 默认 单核并发5个， 最大 20 
                //WorkerCount = Math.Max(Environment.ProcessorCount, 10)
            };
            app.UseHangfireServer(jobOptions);//启动Hangfire服务
            #endregion
        }

    }
}
